package com.vpen.iot.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.text.CharPool;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.vpen.iot.model.dto.AiModelTrainPicJsonDTO;
import com.vpen.iot.utils.TrainFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Vpen
 * @date 2024/9/22 10:17
 */
@Slf4j
@RequestMapping("/test")
@RestController
public class TestController {

    @GetMapping("/url")
    public void parse(@RequestParam(value = "url") String url){
        parseFileZip(url);
    }
    private void  parseFileZip(String fileUrl){
        String dirPath = null;
        try {
            String dir = RandomUtil.randomString(12);
             dirPath = TrainFileUtil.genPath(TrainFileUtil.TEMP_PATH, dir);
            String zipFileName = RandomUtil.randomString(12)+".zip";
            String zipFileNamePath = TrainFileUtil.genFilePath(dirPath, zipFileName);
            File zipFile = TrainFileUtil.downloadImage(fileUrl, zipFileNamePath);
            if (Objects.isNull(zipFile)){
                log.warn("下载失败");
                return;
            }
            File imgZip;
            try {
                imgZip = ZipUtil.unzip(zipFile, Charset.forName("GBK"));
            } catch (UtilException e) {
                imgZip = ZipUtil.unzip(zipFile);
            }
            if (Objects.isNull(imgZip)){
    
                log.warn("解压失败");
                return;
            }
            String parent = imgZip.getPath();
            String annotationsPath = TrainFileUtil.genFilePath(parent, "annotations");
            String imagesPath = TrainFileUtil.genFilePath(parent, "images");

            String labelsFileName = TrainFileUtil.genFilePath(parent, "labels.txt");
            List<String> labelCodeList = FileUtil.readLines(FileUtil.newFile(labelsFileName), CharsetUtil.CHARSET_UTF_8);
            //TODO: 查询数据库是否有这几个标签，少了报错

            // 解析annotationsPath的所有文件，用map存
            Map<String, AiModelTrainPicJsonDTO> jl = parseAnnotations(annotationsPath);
        } catch (UtilException e) {
            e.printStackTrace();
        } catch (IORuntimeException e) {
            e.printStackTrace();
        }finally {
            FileUtil.del(dirPath);
        }


    }





    private Map<String, AiModelTrainPicJsonDTO> parseAnnotations(String dirPath){
        File annotationsDir = FileUtil.newFile(dirPath);
        List<File> files = FileUtil.loopFiles(annotationsDir);
        if (CollUtil.isEmpty(files)) {
            log.warn("当前解析的annotations文件夹为空");
            return  null;
        }
        Map<String, AiModelTrainPicJsonDTO> map = new HashMap<>(16);
        for (File file:files){
            String fileName = getFileName(file);
            String json = FileUtil.readUtf8String(file);
            JSONObject obj = JSONUtil.parseObj(json);
            obj.remove("imageData");
            obj.remove("flags");


            AiModelTrainPicJsonDTO bean = JSONUtil.toBean(obj, AiModelTrainPicJsonDTO.class);


            bean.setImageData(null);
            map.put(fileName,bean);
        }
        return map;
    }

    private String getFileName(File file){
        return StrUtil.subBefore(FileUtil.getName(file), CharPool.DOT,true);
    }
}
